package com.smarthome.web.property.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/httpmsg")
public class HttpStatusController {

    @RequestMapping("/{statusCode}/status")
    public String statusCodeError(@PathVariable Integer statusCode, HttpServletRequest request, HttpServletResponse response) {
        if (statusCode == 400) {
            setErrorResult(request, String.valueOf(statusCode), "请求数据错误，请稍后重试");
            return "/layout/error";
        }
        if (statusCode == 404) {
            setErrorResult(request, String.valueOf(statusCode), "系统无法找到资源，请稍后重试");
            return "/layout/error";
        }
        if (statusCode == 403) {
            setErrorResult(request, String.valueOf(statusCode), "资源禁止访问，请稍后重试");
            return "/layout/error";
        }
        if (statusCode == 405) {
            setErrorResult(request, String.valueOf(statusCode), "资源禁止访问，请稍后重试");
            return "/layout/error";
        }
        if (statusCode == 505) {
            setErrorResult(request, String.valueOf(statusCode), "不支持当前浏览器版本，请稍后重试");
            return "/layout/error";
        }
        // 500及其他
        setErrorResult(request, String.valueOf(statusCode), "系统发生异常，请稍后重试");
        return "/layout/error";

    }

    private void setErrorResult(HttpServletRequest request, String code, String msg) {
        request.setAttribute("code", code);
        request.setAttribute("msg", msg);
    }

}
